package com.yidu.hbatsft.service.impl;

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsft.dao.AtsBaseSportMapper;
import com.yidu.hbatsft.dao.AtsEmpCoachAndUserMapper;
import com.yidu.hbatsft.dao.AtsEmpCoachMapper;
import com.yidu.hbatsft.domain.AtsBaseSport;
import com.yidu.hbatsft.domain.AtsEmpCoach;
import com.yidu.hbatsft.domain.AtsEmpCoachAndUserVo;
import com.yidu.hbatsft.domain.AtsPlanItem;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.hbatsft.service.AtsSchemeService;
import com.yidu.privilege.Dao.RoleDao;
import com.yidu.privilege.Dao.UserAndroleMapper;
import com.yidu.privilege.Dao.UserDao;
import com.yidu.privilege.Domain.Role;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.Domain.UserAndrole;
import com.yidu.utils.FileTools;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Service
public class AtsEmpCoachServiceImpl implements AtsEmpCoachService {
    @Resource
    AtsEmpCoachMapper atsempcoachmapper;

    @Resource
    AtsEmpCoachAndUserMapper coachAnduserMapper;
    @Resource
    private UserDao userDao;//关联用户表

    @Resource
    AtsBaseSportMapper sportMapper;//专业

    @Resource
    UserAndroleMapper userandroleMapper;//角色用户

    @Resource
    RoleDao roleDao;//角色表
    /**
     * 文件上传工具
     */
    @Resource
    FileTools fileTools;
    @Override
    public List<AtsEmpCoach> findAll() {
        return atsempcoachmapper.findAll();
    }

    @Override
    public List<AtsEmpCoach> selAllCoachName() {
        return atsempcoachmapper.selAllCoachName();
    }

    /**
     * 根据ID 查询教练员
     * @param id
     * @return
     */
    @Override
    public AtsEmpCoach findById(Integer id) {
        return atsempcoachmapper.findById(id);
    }

    @Override
    public AtsEmpCoach selCoachId(String otherName) {
        return atsempcoachmapper.selCoachId(otherName);
    }

    @Override
    public AtsEmpCoachAndUserVo selectById(Integer id) {

        return coachAnduserMapper.findById(id);
    }

    @Override
    public int upOrInsertUserCoachDate(AtsEmpCoachAndUserVo vo,MultipartFile file) {
        User user = new User();
        AtsEmpCoach coach = new AtsEmpCoach();
        coach.setCoachName(vo.getCoachName());//设置教练名称
        coach.setDeptName(vo.getDeptName());//设置部门名称
        coach.setPostName(vo.getPostName());//设置岗位名称
        coach.setExpertSpecia(vo.getExpertSpecia());//设置专业
        coach.setAddr(vo.getAddr());//设置地址
        coach.setOther(vo.getOther());//设置其他说明
        coach.setPhone(vo.getPhone());//设置电话
        Integer sportId = Integer.valueOf(vo.getExpertSpecia());//转换
        AtsBaseSport sportinfo = sportMapper.findById(sportId);//根据ID查询到相关名字
        //System.out.println(sportinfo.getSportName()+"=========qeqeqe============");
        coach.setCoachExp(sportinfo.getSportName());//擅长项目
        coach.setOrganName(vo.getOrganName());//所在公司
        coach.setIsva(1);//设置有效
        coach.setCreatTime(new Date());//设置创建时间



        user.setUsName(vo.getUsName());//设置登录账号
        user.setUsPass(vo.getUsPass());//设置登陆密码
        user.setUsPhone(vo.getPhone());//设置电话
        user.setUsRemark(vo.getOther());//设置其他说明
        user.setUsType("教练");//设置职位
        user.setUsState("0");//设置状态
        //判断文件非空时
        if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
            //处理异常
            try {
                String usPhoto = fileTools.saveFile("UserImg", file);
                user.setUsPhoto(usPhoto);//设置头像
                System.err.println("===================" + usPhoto);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        user.setUsEmail(vo.getUsEmail());//设置邮箱
        System.out.println("方法前的教练ID："+vo.getCoachId());
        //如果ID为空则进入新增，如果为空则进入修改
        if (vo.getCoachId() == null) {
            System.out.println("进入新增");
            //插入到教练表
            int insert = atsempcoachmapper.insert(coach);
            user.setUsWaiid(coach.getCoachId());//设置外表ID
            if(insert>0){
                //插入用户表
               int userinsert = userDao.insert(user);
               if(userinsert>0){
                   //获取教练ID及角色表ID，插入角色用户表
                   Integer coachId = coach.getCoachId();
                   Integer userId = user.getUsId();
                   //查询角色表中属于教练角色的ID
                   Role roleInfo = roleDao.queryIDbyname();
                   UserAndrole userandrole = new UserAndrole();
                   userandrole.setRoId(roleInfo.getRoId());
                   userandrole.setUsId(userId);
                   userandroleMapper.insert(userandrole);
                   System.err.println(coachId+"============="+userId+"=============");
                   System.out.println("新增完毕");
                   return insert;
               }
            }

        } else {
            System.out.println("进入修改");
            coach.setCoachId(vo.getCoachId());//设置教练员ID
            user.setUsId(vo.getUsId());//设置用户ID
            int count = atsempcoachmapper.updateByPrimaryKey(coach);
            if (count > 0) {

                int s = userDao.update(user);
                if (s > 0) {
                    System.out.println("修改完毕");
                    return count;
                }
            }

        }

        return  0;
    }

    /**
     * 加载专业选项
     * @return
     */
    @Override
    public List<AtsBaseSport> loadSportName() {

        return sportMapper.selectAll();
    }

    @Override
    public int deleteById(Integer coachId) {
        int cout = atsempcoachmapper.deleteByPrimaryKey(coachId);
        if(cout >0){
            userDao.deleteByid(coachId);
        }
        return cout;
    }

    /**
     * 分页插件查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<AtsEmpCoach> findAllX(Integer pageNum,Integer pageSize,String coachName) {
        PageHelper.startPage(pageNum,pageSize);
        return atsempcoachmapper.findAllX(coachName);
    }

    /**
     * 查询每页的条目数
     * @return
     */
    @Override
    public int count() {
        return atsempcoachmapper.count();
    }
}
